<?php

/**
 * 生成省市区联动JS数据
 * 
 * @param type $data
 * <code>
  $data = array(
  array('Province' => '北京', 'City' => '北京市', 'Area' => '昌平区'),
  array('Province' => '北京', 'City' => '北京市', 'Area' => '海淀区'),
  );
 * </code>
 * @param type $str
 * @return type
 */
function getPzData($data, $str = '') {
	$tmp_data = array();
	foreach ($data as $vo) {
		$tmp_data[$vo['Province']][$vo['City']][$vo['Area']] = '';
	}
	foreach ($tmp_data as $key => $vo) {
		$str .= "{$key}\$请选择城市,请选择地区";
		foreach ($vo as $k => $subvo) {
			$str.="|{$k},请选择地区," . join(',', array_keys($subvo));
		}
		$str.='#';
	}
	unset($tmp_data);
	return trim($str, '#');
}

function index() {
	// new PCAS("Province","City","Area","吉林省","松原市","宁江区")
	$data = array(
		array(
			'Province'	 => '北京',
			'City'		 => '北京市',
			'Area'		 => '昌平区'
		),
		array(
			'Province'	 => '北京',
			'City'		 => '北京市',
			'Area'		 => '海淀区'
		),
		array(
			'Province'	 => '广东省',
			'City'		 => '广州市',
			'Area'		 => '海珠区'
		),
		array(
			'Province'	 => '广东省',
			'City'		 => '深圳市',
			'Area'		 => '南山区'
		),
		array(
			'Province'	 => '广东省',
			'City'		 => '深圳市',
			'Area'		 => '罗湖区'
		),
	);
	// HTML代码
	echo "	<select name='Province'></select>
			<select name='City'></select>
			<select name='Area'></select>";
	// JS代码
	echo '<script>var PCAD="' . getPzData($data) . '"</script>';
	echo '<script src="pcasunzip.js"></script>';
	echo '<script>new PCAS("Province", "City", "Area")</script>';
}

index();
